package com.example.lt.service.impl;

import com.example.lt.config.QiNiuConfig;
import com.example.lt.service.QiNiuFileService;
import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 小涛Tao
 * @Date: 2024/08/05/11:29
 * version 1.0 注释：
 **/
@Service
public class QiNiuFileServiceImpl implements QiNiuFileService {

    @Autowired
    private QiNiuConfig qiNiuConfig;

    /**
     * 从 QiNiu 上获取文件信息
     * @param fileKey 文件在存储桶中的唯一标识符
     * @return
     */
    @Override
    public FileInfo getFileInfo(String fileKey) {
        Configuration config = new Configuration(Region.region2());
        final Auth auth = qiNiuConfig.buildAuth();
        final String bucketName = qiNiuConfig.getBucketName();
        // 创建 BucketManager 用于管理存储桶中的文件
        BucketManager bucketManager = new BucketManager(auth, config);
        try {
            // 获取到文件的信息
            FileInfo fileInfo = bucketManager.stat(bucketName, fileKey);
            return fileInfo;
        } catch (QiniuException ex) {
            System.err.println(ex.response.toString());
        }
        return null;
    }

}
